# Generate Figure 5


## Rerun the main model

use.data <- data_all[!is.na(data_all$Chosen_party), ] %>%
  rename(psid = ID)

results <- amce(Chosen_party ~ Economy + Jobs + Immigration + EU + Education,
                data = use.data, design = ukdesign, cluster=F)
source("01_sub1_process_results.R")
amce_plot_data_all_data <- amce_plot_data %>% mutate(data = "All")

## Set up the conditions for count unlikely options
tmp <- use.data %>% select(c(Education, EU, Immigration, Jobs)) 
tmp_num <- tmp %>% 
  mutate(Education = as.numeric(Education),
         EU = as.numeric(EU),
         Immigration = as.numeric(Immigration),
         Jobs = as.numeric(Jobs))

## check the proportion of unlikely options
### we use labour_count > 0 & cons_count > 0 (i.e. mixture of labour and conservative)

partypolicies_num <- gdata::read.xls("Policy_Table.xlsx") %>%
  .[, 2:5] %>%  mutate_all(~factor(.) %>% as.numeric)
real_policy_combination <- partypolicies_num %>% apply(1, paste, collapse = "")
tmp_num %>% apply(1, paste, collapse = "") %>% `%in%`(real_policy_combination) %>% table %>% prop.table

cons_count <- apply(tmp_num, 1, function(x) sum(x == 1))
labour_count <- apply(tmp_num, 1, function(x) sum(x[1] == 2, 
                                                      x[2] == 3,
                                                      x[3] == 5,
                                                      x[4] == 3))
table(cons_count, labour_count) 
table(cons_count > 0, labour_count > 0)

## Now generate the unlikely option model and rerun the model
unlikely.data <- use.data %>% filter(cons_count > 0 & labour_count > 0)
results <- amce(Chosen_party ~ Economy + Jobs + Immigration + EU + Education,
                data = unlikely.data, design = ukdesign, cluster=F)
source("01_sub1_process_results.R")
amce_plot_data_unlikely_options <- amce_plot_data %>% mutate(data = "Unlikely Options")

## Combine the two predictions from main model and unlikely options, and plot (Figure 5)
amce_plot_data_both_samples <- bind_rows(amce_plot_data_unlikely_options, 
                                         amce_plot_data_all_data)

g <- ggplot(amce_plot_data_both_samples, aes(y = AMCE, x = texts,
                                             group = data, colour = data)) + 
  geom_point(position = position_dodge(width = .7)) + 
  geom_errorbar(aes(ymin = AMCE - 1.96 * se,
                    ymax = AMCE + 1.96 * se, width = 0),
                position = position_dodge(width = .7)) + 
  geom_vline(xintercept = 0, size = 1, color = "grey90") + 
  scale_color_manual(values = c("gray70", "black")) +  
  coord_flip() +
  xlab("") + ylab("AMCE") + 
  theme(axis.text.y = element_text(hjust = 0),
        legend.position = "bottom",
        legend.title = element_blank()) 
ggsave(plot = g, "figure5.pdf", width= 7, height = 5)

